<?php

namespace Bean\Db;

class User extends \Bean\User {

	private $_data;
	private $_isContact;
	private $_requests = array();
	private static $_users;
	
	public static function get( $user_id ) {
		if( !isset(self::$_users[$user_id]) ) {
			$user = _q()->get1( 'users', array( 'user_id' => $user_id ) );
			self::$_users[$user_id] = new self( $user );
		}
		return self::$_users[$user_id];
	}
	
	public static function getUser( $user ) {
		return new self( $user );
	}

	public function __construct( $data ) {
		parent::__construct();
		$this->_data = $data;
	}

	public function isContact( User $user = null ) {
		$user = _u( $user );
		if( !$user ) {
			return false;
		}
		$user_id = $user->getUserId();
		if( !isset($this->_isContact[$user_id]) ) {
			$n_contacts = _q()->getC( 'users_contacts', array(
				'user_id' => $user_id,
				'contact_id' => $this->getUserId()
			));
			$this->_isContact[$user_id] = $n_contacts ? true : false;
		}
		return $this->_isContact[$user_id];
	}
	
	public function isContactable( User $user = null ) {
		$user = _u( $user );
		if( !$user ) {
			return false;
		}
		return $this->getUserId() != $user->getUserId();
	}
	
	public function isAwaitingConfirmation( User $user = null ) {
		$user = _u( $user );
		if( !$user ) {
			return false;
		}
		return _q()->getC( 'users_requests', array(
			'requester_id' => $this->getUserId(),
			'acceptor_id' => $user->getUserId(),
			'accepted' => 0
		) ) > 0;
		
	}
	
	public function getUserId() {
		return $this->_data['user_id'];
	}
	
	public function getFlowId() {
		return $this->_data['flow_id'];
	}
	
	public function getFirstName() {
		return $this->_data['first_name'];
	}
	
	public function getLastName() {
		return $this->_data['last_name'];
	}
	
	public function getFullName() {
		return $this->getFirstName() . ' ' . $this->getLastName();
	}
	
	public function getUrl() {
		return $this->isMe() ? './' : './User:' . $this->getUserId() . ':' . $this->getUrlName();
	}
	
	public function getUrlName() {
		return strtr( str_replace( '.', '', $this->getFullName() ), ' ', '_' );
	}
	
	public function getEmail() {
		return $this->_data['email'];
	}
	
	private function _getImage( $folder = null ) {
		if( $folder === null ) {
			return 'default';
		}
		$conf = \Library\Config::get( 'path' );
		return $conf['images']['users'] . $folder . '/' . $this->_getImage() . '.jpg';
	}
	
	public function getShortcutIcon() {
		return $this->_getImage( '16x16' );
	}
	
	public function getImagePath() {
		return $this->_getImage( 'profile' );
	}
	
	public function getThumbPath() {
		return $this->_getImage( '50x50' );
	}
	
	public function getMiniPath() {
		return $this->_getImage( '32x32' );
	}
	
	public function setName( $first_name, $last_name ) {
	
		$this->_data['first_name'] = $first_name;
		$this->_data['last_name'] = $last_name;
		
		_q()->upt( 'users', array(
				'first_name' => $first_name,
				'last_name' => $last_name
			), array( 'user_id' => $this->getUserId() ) );
			
		return $this;
	}
	
	public function setEmail( $email ) {
	
		$this->_data['email'] = $email;
		
		_q()->upt( 'users', array(
				'email' => $email
			), array( 'user_id' => $this->getUserId() ) );
			
		return $this;
	}
	
	public function getRequest( User $user = null ) {
		$user = _u($user);
		if( !$user ) {
			return false;
		}
		
		$user_id = $user->getUserId();
		if( !isset($this->_requests[$user_id]) ) {
			$request = _q()->get1( 'users_requests', array(
				'requester_id' => $this->getUserId(),
				'acceptor_id' => $user_id ) );
			$this->_requests[$user_id] = new User\Request( $request );
		}
		return $this->_requests[$user_id];
	}

}
